﻿using System;
using fastCSharp;
using fastCSharp.code.cSharp;

namespace diantou.web.contest
{
    /// <summary>
    /// 用户比赛
    /// </summary>
    [fastCSharp.code.cSharp.webView.clientType(Name = "diantou.userContest")]
    struct userContest
    {
        /// <summary>
        /// 相关收藏夹
        /// </summary>
        public struct relateFavorite
        {
            /// <summary>
            /// 是否重新加载视图
            /// </summary>
            [fastCSharp.code.cSharp.webView.outputAjax]
            public bool ViewOnly;
            /// <summary>
            /// 用户比赛
            /// </summary>
            public userContest Contest;
            /// <summary>
            /// 相关收藏top5
            /// </summary>
            private diantou.dataAccess.favorite[] top5;
            /// <summary>
            /// 相关收藏top5
            /// </summary>
            public diantou.dataAccess.favorite[] Top5
            {
                get
                {
                    if (ViewOnly) return null;
                    if (top5 == null)
                    {
                        int count;
                        fastCSharp.sql.logCache<int, keyValue<int[], int>>.array.value cacheValue = favorite.Cache.Get(Contest.Contest.Id);
                        if (cacheValue.Value.Key == null)
                        {
                            cacheValue.Value.Key = diantou.dataAccess.tcpCall.favorite.relateIds(dataModel.favorite.type.Contest, Contest.Contest.Id, fastCSharp.config.pub.Default.PageSize, 1, Contest.UserId, out cacheValue.Value.Value);
                        }
                        diantou.dataAccess.favorite[] values = diantou.dataAccess.clientCache.Favorite.Get(cacheValue.Value.Key);
                        favorite.SetCache(values, ref cacheValue);
                        top5 = values.sub(0, 5).ToArray();
                        if (top5 == null) top5 = nullValue<diantou.dataAccess.favorite>.Array;
                    }
                    return top5;
                }
            }
        }
        /// <summary>
        /// 比赛
        /// </summary>
        public diantou.dataAccess.contest Contest;
        /// <summary>
        /// 用户信息
        /// </summary>
        public diantou.dataAccess.user User;
        /// <summary>
        /// 比赛标识
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        public int Id
        {
            get { return Contest == null ? 0 : Contest.Id; }
        }
        /// <summary>
        /// 用户标识
        /// </summary>
        internal int UserId
        {
            get { return User == null ? 0 : User.Id; }
        }
        ///// <summary>
        ///// 用户是否关注比赛
        ///// </summary>
        //public bool IsFocus
        //{
        //    get
        //    {
        //        return User != null && Contest != null && diantou.dataAccess.tcpCall.focusContest.IsFocus(User.Id, Contest.Id, dataModel.focus.type.Focus);
        //    }
        //}
        /// <summary>
        /// 用户是否注册比赛
        /// </summary>
        public bool IsRegister
        {
            get
            {
                return User != null && Contest != null && Contest.CanJoin && diantou.dataAccess.clientCache.ContestRegister[new dataModel.focus.primaryKey { UserId = User.Id, FocusId = Contest.Id }] != DateTime.MinValue;
            }
        }
        /// <summary>
        /// 用户在比赛中的排行
        /// </summary>
        private int? rank;
        /// <summary>
        /// 用户在比赛中的排行
        /// </summary>
        public int Rank
        {
            get
            {
                if (User == null || Contest == null || Contest.Type == dataModel.contest.type.External) return 0;
                if (rank == null) rank = diantou.dataAccess.tcpCall.contestJudge.GetRank(Contest.Id, User.Id);
                return (int)rank;
            }
        }
        /// <summary>
        /// 用户在比赛中的AC分数
        /// </summary>
        public int AcceptedPoint
        {
            get
            {
                if (Rank == 0 || Contest.Type == dataModel.contest.type.External) return 0;
                return diantou.dataAccess.tcpCall.contestJudge.getAcceptedPoint(Contest.Id, User.Id);
            }
        }
        /// <summary>
        /// 提交记录地址
        /// </summary>
        public webView.hashUrl SubmitPath
        {
            get
            {
                if (Contest == null || Contest.Type == dataModel.contest.type.External) return new webView.hashUrl { Path = diantou.dataModel.path.pub.Default.ContestListPath };
                if (User == null || !IsRegister) return Contest.Path.JudgeList;
                return new diantou.dataModel.path.contest { Id = Contest.Id }.ContestSubmit(User.Id);
            }
        }
        /// <summary>
        /// 用户是否比赛所有者
        /// </summary>
        public bool IsOwner
        {
            get
            {
                if (User != null && Contest != null)
                {
                    if (User.Permission == dataModel.user.permission.Admin || Contest.UserId == User.Id) return true;
                    if (dataAccess.clientCache.ContestOwner.IsData) return dataAccess.clientCache.ContestOwner[new dataModel.contestOwner.primaryKey { ContestId = Contest.Id, UserId = User.Id }];
                    return diantou.dataAccess.tcpCall.contestOwner.isUser(Contest.Id, User.Id);
                }
                return false;
            }
        }
    }
}
